<template>
  <div className="hl-group-item">
    <Upload :showUploadList="false" :customRequest="handleImport">
      <Button type="link">
        <Icon icon="bi:download" />
        导入
      </Button>
    </Upload>
  </div>
</template>
<script name="imports" setup>
  import { Button } from 'ant-design-vue';
  import { Icon } from '/@/components/Icon';
  import { uploadDataFromOuterBpmn } from '/@/api/config-center/bpmn-process';
  import { Upload } from 'ant-design-vue';
  import modeler from '/@/store/modules/bpmnModeler';
  import { useMessage } from '/@/hooks/web/useMessage';
  const { createMessage } = useMessage();
  const modelerStore = modeler();
  async function handleImport({ file }) {
    try {
      const res = await uploadDataFromOuterBpmn({ file });
      if (res.data.code === 0) {
        const activities = res.data?.data?.bpmnData?.activities;
        const flowInfo = res.data?.data?.bpmnData?.flowInfo;
        activities.forEach((item) => {
          item.R0 = [];
          item.rangeType = item.participant?.rangeType;
          item.rangeComponentType = item.participant?.rangeComponentType;
          item.selectUserNum = item.participant?.selectUserNum;
          item.maxLimitedNum = item.participant?.maxLimitedNum;
          item.caculateReturnFlag = item.participant?.caculateReturnFlag;
          item.caculateFormula = item.participant?.caculateFormula;
          item.participantRange = item.participant?.participantRange;
          item.transLimit = item.handleContent?.transLimit;
          item.compareCont = item.handleContent?.compareCont;
          item.limitCompare = item.handleContent?.limitCompare;
          item.transLimitChoice = item.handleContent?.transLimitChoice;

          item.rights = [];
          if (item?.baseInfo?.rights && item?.baseInfo?.rights.length > 0) {
            item.rights = item?.baseInfo?.rights;
          }
          if (!item.remindAddFlag) {
            item.remindAddFlag = [];
          }
          const r = item.participant?.settings?.find((i) => i.type === 'R0');

          if (r) {
            item.R0 =
              r.participants?.map((i) => ({ value: i.id * 1, userCode: i.code, label: i.name })) ||
              [];
          } else {
            item.R0 = [];
          }
          const participants =
            item.participant?.settings
              ?.filter((i) => i.type !== 'R0')
              .map((el) => el.participants) || [];
          item.participants = participants.flat();
        });

        modelerStore.setActivities(activities);
        modelerStore.getModeler?.importXML(res?.data?.data?.bpmnXml || '');
        modelerStore.setFlowInfo(flowInfo || {});

        createMessage.success('导入成功');
      } else {
        createMessage.error(res.data.msg);
      }
    } catch (err) {
      console.error(err);
    } finally {
    }
  }
</script>
